﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;

namespace ModuleDuoc.Report
{
    public partial class frmTK_ThuocTralai_New : ModuleDuoc.Base.frmTK_Base
    {
        public frmTK_ThuocTralai_New()
        {
            InitializeComponent();
            this.dgvTraLai.AutoGenerateColumns = false;
            this.dgvTraLaiCT.AutoGenerateColumns = false;
            date1.Text = this.date1.Value.ToString("dd/MM/yyyy 00:00:00");
            date2.Text = this.date2.Value.ToString("dd/MM/yyyy 23:59:59");
            loadThuocTraLai();
        }
        private void loadThuocTraLai() // Lấy danh sach thuoc khach hang tra lai tu phieu tra lại 
        {
            string sql = "SELECT zdptl.PhieuTraLai_ID, zdptl.NgayLap, zdptl.TenNhanVien, zdptl.TongTien, " +
                        "zdptl.SoHoaDonBanLe, zdptl.GhiChu, zdptl.KhachHang, zdptl.DienThoai,zdptl.DiaChi " +
                        "FROM zD_PhieuTraLai zdptl " +
                        "Where ngaylap BETWEEN CONVERT (DATETIME, '" + this.date1.Value.ToString("dd/MM/yyyy 00:00:00") + "', 103) " +
                        "AND CONVERT (DATETIME, '" + this.date2.Value.ToString("dd/MM/yyyy 23:59:59") + "', 103) " +
                        "ORDER BY ngaylap";

            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.dgvTraLai.DataSource = dt;         
        }
        private void loadThuocTraLaiCT() // Lấy danh sach thuoc khach hang tra lai CT tu phieu tra lại CT
        {
            string sql = "SELECT zdptlc.MaVach, " +
                       "zdptlc.TenVatTu, zdptlc.SoLo, zdptlc.DonViTinh, zdptlc.SoLuongMua, " +
                       "zdptlc.DonGia, zdptlc.SoLuongTraLai, zdptlc.ThanhTien, zdptlc.GhiChu, " +
                       "zdptlc.DuyetTraLai " + 
                       "FROM zD_PhieuTraLaiCT zdptlc " +
                       "WHERE zdptlc.SoHoaDonBanLe = N'"+dgvTraLai.Rows[dgvTraLai.CurrentRow.Index].Cells["SoHoaDonBanLe"].Value.ToString()+"'";

            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.dgvTraLaiCT.DataSource = dt;
        }

        private void dgvTraLai_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            loadThuocTraLaiCT();
            tongTien();
        }

        public override void btnView_Click(object sender, EventArgs e)
        {
            base.btnView_Click(sender, e);
            loadThuocTraLai();
        }
        private void tongTien()
        {
            float tt = 0;
            for (int i = 0; i < dgvTraLaiCT.Rows.Count; i++)
            {
                tt += float.Parse(dgvTraLaiCT.Rows[i].Cells["ThanhTien"].Value.ToString()); 
            }
            txtTongTien.Text = tt.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            printReport();
        }
        private void printReport()
        {
            try
            {
                string sql = "SELECT * FROM [QUANLYVTXN].[thongke].[hien_rpt_TraLai] " +
                             "WHERE SoHoaDonBanLe = N'" + dgvTraLai.Rows[dgvTraLai.CurrentRow.Index].Cells["SoHoaDonBanLe"].Value.ToString() + "'";
                DataTable dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                frmShowReport frm = new frmShowReport(2);
                frmShowReport._thanhtien = lblBangChu.Text;
                frmShowReport._date1 = String.Format("{0:dd/MM/yyyy}", DateTime.Now.ToShortDateString());
                frmShowReport._dt = dt;
                frmShowReport.strC = frmShowReport.BC.PHIEUTRALAI;
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi không có dữ liệu", "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.lblBangChu.Text = DICHSO.XL_DOC_SO.DOC_SO_CHUOI(Double.Parse(this.txtTongTien.Text.ToString()), "đồng", "", 0);
        }
        public override void btnExportReport_Click(object sender, EventArgs e)
        {
            base.btnExportReport_Click(sender, e);
            string sql = "select * from thongke.hien_rpt_TraLaiNew " +
                        "where ngaylap BETWEEN CONVERT (DATETIME, '" + this.date1.Value.ToString("dd/MM/yyyy 00:00:00") + "', 103) " +
                        "AND CONVERT (DATETIME, '" + this.date2.Value.ToString("dd/MM/yyyy 23:59:59") + "', 103) " + 
                        " ORDER BY ngaylap";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            frmShowReport frm = new frmShowReport(2);
            frmShowReport._date1 = this.date1.Text.ToString();
            frmShowReport._date2 = this.date2.Text.ToString();
            frmShowReport._dt = dt;
            frmShowReport.strC = frmShowReport.BC.TKTHUOCTRALAI;
            frm.ShowDialog();
        }
    }
}
